<template>
  <div>
    <van-nav-bar :title="$route.query.key + ' 的搜索结果'" />

    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell
        v-for="item in list"
        :key="item.art_id"
        :title="item.title"
        @click="$router.push({ name: 'detail', query: { id: item.art_id } })"
      />
    </van-list>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
    };
  },
  methods: {
    async onLoad() {
      // 发请求获取搜索结果
      let res = await this.$axios({
        url: "/v1_0/search",
        params: {
          page: this.page,
          per_page: 10,
          q: this.$route.query.key,
        },
      });

      this.list.push(...res.data.data.results);
      this.loading = false;
      // 每次加载完毕让page++
      this.page++;

      // 当数组数量大于或等于总数量，就代表加载完了
      if (this.list.length >= res.data.data.total_count) {
        this.finished = true;
      }
    },
  },
};
</script>

<style>
</style>